package com.ruoyi.oar.controller.pc;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ToolUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oar.domain.dto.OarHouseDTO;
import com.ruoyi.oar.domain.query.OarHouseQuery;
import com.ruoyi.oar.service.OarHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 房屋Controller
 * 
 * @author ruoyi
 * @date 2022-02-17
 */
@Api(tags = "PC端房屋信息")
@RestController
@RequestMapping("/pc/oar-houses")
public class PcHouseController extends BaseController
{
    @Autowired
    private OarHouseService oarHouseService;

    /**
     * 查询房屋列表
     */
    @ApiOperation("查询房屋列表")
    @GetMapping
    public TableDataInfo<OarHouseDTO> list(OarHouseQuery oarHouseQuery){
        logger.debug("查询房屋列表,查询条件oarHouseQuery={}",JSONObject.toJSONString(oarHouseQuery));
        startPage();
        return oarHouseService.findAll(oarHouseQuery);
    }

    /**
     * 导出房屋列表
     */
    @ApiOperation("导出房屋列表")
    @Log(title = "房屋", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OarHouseQuery oarHouseQuery){
        logger.debug("查询房屋列表,导出条件oarHouseQuery={}",JSONObject.toJSONString(oarHouseQuery));
        List<OarHouseDTO> list = oarHouseService.selectOarHouseList(oarHouseQuery);
        ExcelUtil<OarHouseDTO> util = new ExcelUtil<OarHouseDTO>(OarHouseDTO.class);
        util.exportExcel(response,list, "房屋数据");
    }

    /**
     * 获取房屋详细信息
     */
    @ApiOperation("获取房屋详细信息")
    @GetMapping(value = "/{id}")
    public Response<OarHouseDTO> getInfo(@PathVariable("id") @ApiParam("房屋ID") String id) {
        logger.debug("获取房屋详细信息,id={}",id);
        checkId(id);
        return Response.success(oarHouseService.selectOarHouseById(id).orElseThrow(()->new ServiceException("无效的ID")));
    }

    /**
     * 新增房屋
     */
    @ApiOperation("新增房屋")
    @Log(title = "房屋", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody @Validated OarHouseDTO oarHouseDTO) {
        logger.debug("新增房屋,oarHouseDTO={}",JSONObject.toJSONString(oarHouseDTO));
        return toAjax(oarHouseService.insertOarHouse(oarHouseDTO,1));
    }

    /**
     * 修改房屋人员房间信息
     */
    @ApiOperation("修改房屋人员房间信息")
    @Log(title = "房屋", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OarHouseDTO oarHouseDTO) {
        logger.debug("修改房屋人员房间信息,oarHouseDTO={}",JSONObject.toJSONString(oarHouseDTO));
        checkId(oarHouseDTO.getId());
        return toAjax(oarHouseService.updateOarHouse(oarHouseDTO,1));
    }

    /**
     * 修改房屋
     */
    @ApiOperation("网约房审核")
    @Log(title = "网约房审核", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/audit")
    public AjaxResult audit(@RequestParam(required = false) @ApiParam("房屋id")  String[] ids,@RequestParam(required = false) @ApiParam("审核状态 1:审核通过 2:审核不通过") Long auditState) {
        if (ToolUtil.isEmpty(ids)) {
            throw new ServiceException("ids不能为空");
        }
        if (ToolUtil.isEmpty(auditState)) {
            throw new ServiceException("auditState不能为空");
        }
        OarHouseQuery oarHouseQuery = new OarHouseQuery();
        oarHouseQuery.setIds(ids);
        oarHouseQuery.setAuditState(auditState);
        return toAjax(oarHouseService.batchUpAuditState(oarHouseQuery));
    }

    /**
     * 删除房屋
     */
    @ApiOperation("删除房屋")
    @Log(title = "房屋", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        logger.debug("删除房屋,ids={}",JSONObject.toJSONString(ids));
        Arrays.asList(ids).forEach(this::checkId);
        return toAjax(oarHouseService.deleteOarHouseByIds(ids));
    }

    /**
     * 查询标准地址
     */
    @ApiOperation("查询标准地址")
    @GetMapping(value = "/getDzbm")
    public AjaxResult getDzbm(String addr){
        if (ToolUtil.isEmpty(addr)) {
            throw new ServiceException("addr不能为空");
        }
        return AjaxResult.success(null);
    }

    @ApiOperation("统计网约房信息审核数量")
    @GetMapping(value = "countByAuditState")
    public AjaxResult countByAuditState(){
        return AjaxResult.success(oarHouseService.countByAuditState(0));
    }

    /**
     * 校验
     * @param id
     */
    private void checkId(String id) {
        Optional.ofNullable(id).orElseThrow(() -> new ServiceException("ID不能为空"));
        oarHouseService.selectOarHouseById(id).orElseThrow(() -> new ServiceException("无效的ID"));
    }
}
